from django.shortcuts import render

import json
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework import status
from utils.jwt_token import check_login
from user.models import *
from .serializer import *
from user.serializer import *
from school.serializer import SchoolSer


# 班级
class ClasssView(ViewSet):

    # 添加班级
    @check_login
    def add_classs(self, request):
        name = request.data.get("name")
        school = request.data.get("school_name")

        if not all([name, school]):
            return Response({"code": status.HTTP_204_NO_CONTENT, "msg": "数据不全"})
        try:
            classs = ClasssModel.objects.filter(name=name, is_delete=False)
            if classs:
                return Response({"code": status.HTTP_204_NO_CONTENT, "msg": "班级已经存在"})

            list = SchoolModel.objects.filter(name=school, is_delete=False)
            ser = SchoolSer(list, many=True).data
            for i in ser:
                if i.get("name") == school:
                    school = i.get("id")
                    print("school", school)

            data = {
                "name": name,
                "school": school
            }
            ser = ClasssSer(data=data)
            if not ser.is_valid():
                return Response({'code': status.HTTP_400_BAD_REQUEST, 'msg': ser.errors})
            ser.save()
            return Response({"code": status.HTTP_200_OK, "msg": "添加成功"})
        except Exception as e:
            print("失败", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg": "失败"})

    # 获取所有班级
    @check_login
    def list_classs(self, request):
        try:
            classs = ClasssModel.objects.filter(is_delete=False).all()
            ser = ClasssSer(classs, many=True).data
            return Response({"code": status.HTTP_200_OK, "msg": "成功", "data": ser})
        except Exception as e:
            print("失败", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg": "失败"})

    # 修改班级
    @check_login
    def up_classs(self, request, pk):
        name = request.data.get("name")

        try:
            lst = ClasssModel.objects.filter(id=pk, is_delete=False)

            if not lst:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg": "没有此学校"})

            if name:
                lst.update(name=name)
            return Response({"code": status.HTTP_200_OK, "msg": "成功"})
        except Exception as e:
            print("失败", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg": "失败"})

    # 删除班级
    @check_login
    def del_classs(self, request, pk):
        try:
            classs = ClasssModel.objects.filter(id=pk, is_delete=False)
            if not classs:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg": "没有此学校"})
            classs.update(is_delete=True)
            return Response({"code": status.HTTP_200_OK, "msg": "成功"})
        except Exception as e:
            print("失败", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg": "失败"})

    # 获取单个班级
    @check_login
    def single_classs(self, request, pk):
        try:
            classs = ClasssModel.objects.filter(id=pk, is_delete=False).first()
            classs = ClasssSer(classs).data
            classs = json.loads(json.dumps(classs))

            user = UserModel.objects.filter(classs_id=classs["id"], is_delete=False).all()

            user_ser = UserSer(user, many=True).data
            users = json.loads(json.dumps(user_ser))
            classs['user_list'] = users

            return Response({"code": status.HTTP_200_OK, "msg": "成功", "data": classs})
        except Exception as e:
            print("失败", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg": "失败"})

    # 获取一个班级下的所有学生
    @check_login
    def list_clbums(self, request, pk):
        try:
            user = UserModel.objects.filter(classs_id=pk, is_delete=False)
            ser = UserSer(user, many=True).data
            return Response({"code": status.HTTP_200_OK, "msg": "获取班级下的所有学生成功", "data": ser})
        except Exception as e:
            print("失败", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg": "失败"})

